//////////////////////////////////////////////////////////////////////////
// ConnecterManager.h													//
//																		//
// WYJ																	//
// 2010.4																//
//																		//
//////////////////////////////////////////////////////////////////////////

#ifndef __CONNECTERMANAGER_H__
#define __CONNECTERMANAGER_H__

#define FD_SETSIZE 2000

#include <afxmt.h>
#include <map>
#include <winsock2.h>

class Connecter;

typedef std::map< DWORD, Connecter * > ThreadMap;
typedef std::map< DWORD, Connecter * >::iterator ThreadMapIter;
typedef std::map< DWORD, Connecter * >::const_iterator ThreadMapConstIter;

typedef std::map< unsigned long, ThreadMap > AddrMap;
typedef std::map< unsigned long, ThreadMap >::iterator AddrMapIter;
typedef std::map< unsigned long, ThreadMap >::const_iterator AddrMapConstIter;

class MessageHandler;
class ConnecterManager
{
public:
	ConnecterManager( void );
	~ConnecterManager( void );

	bool Initialize( void );
	void SetListenSocket( SOCKET sock );
	const SOCKADDR_IN* GetListenAddr( void );
	void AddConnecter( SOCKET sock, const SOCKADDR_IN* addr );
	void SetReceiveThreadID( DWORD threadID );
	int	GetConnecterNum( void );
	Connecter* GetConnecter( int index );
	bool RemoveConnecter( int index );
	void DispatchMessage( int index, const char * msg );
	bool InitConnecter( int index, DWORD threadID );
	void SetHwnd( HWND hWnd );
	Connecter* GetConnecter( unsigned long addr, DWORD threadID );
	void Notify( Connecter* pConnecter );

private:
	DWORD		m_ListenThreadID;
	SOCKET		m_ListenSocket;
	SOCKADDR_IN m_ListenAddr;

	DWORD		m_ReceiveThreadID;

	int			m_ConnecterNum;
	Connecter** m_Connecter;
	int			m_AddrMapNum;
	AddrMap		m_AddrMap;
	

	CMutex		m_Mutex;

	MessageHandler* m_pMessageHandler;

	HWND		m_hWnd;
};

#endif // __CONNECTERMANAGER_H__